<template>
  <div class="app-container">

    <el-row>

      <el-col :span="24">

        <div class="grid-content bg-purple-dark">

          <el-row>

            <el-form :inline="true" :model="queryParams" class="demo-form-inline">

              <el-form-item label="状态">

                <el-select v-model="queryParams.auditStatus" placeholder="请选择" class="custom-select" clearable>

                  <el-option label="已通过" value="1"></el-option>

                  <el-option label="待审核" value="2"></el-option>

                  <el-option label="已拒绝" value="3"></el-option>

                </el-select>

              </el-form-item>

              <el-form-item label="住户类型">

                <el-select v-model="queryParams.residentTypeId" placeholder="请选择" class="custom-select" clearable>


                  <el-option v-for="r in residentTypeList" :key="r.id" :label="r.typeName" :value="r.id"></el-option>

                </el-select>

              </el-form-item>

              <el-form-item label="证件类型">

                <el-select v-model="queryParams.documentTypeId" placeholder="请选择" class="custom-select" clearable>

                  <el-option v-for="d in documentTypeList" :key="d.id" :label="d.documentName" :value="d.id"></el-option>

                </el-select>

              </el-form-item>

              <el-form-item label="证件号">

                <el-input v-model="queryParams.documentNumber"></el-input>

              </el-form-item>

              <el-form-item label="姓名">

                <el-input v-model="queryParams.name"></el-input>

              </el-form-item>
              <el-form-item label="手机号">

                <el-input v-model="queryParams.phoneNumber" ></el-input>

              </el-form-item>

            </el-form>

          </el-row>

          <!-- 按钮 -->

          <el-row>

            <el-button type="success" style="margin-right: 10px;width: 140px" @click="getList()">查询</el-button>

            <el-button type="primary" style="margin-right: 10px;width: 140px" @click="chongzhi()">一键重置</el-button>

            <!--            <el-button type="success" style="margin-right: 10px;" @click="handleAdd()" >新增</el-button>-->



            <!--            <el-upload action="http://localhost:5959/p-account/account/importExcel" :show-file-list="false"-->

            <!--                       :on-success="handleAvatarSuccess1" :before-upload="beforeAvatarUpload1" name="file"-->

            <!--                       style="display: inline-block; margin-right: 10px;">-->

            <!--              <el-button type="primary" @click="onSubmit">-->

            <!--                导入-->

            <!--              </el-button>-->

            <!--            </el-upload>-->



            <!--            <el-button type="info" style="margin-right: 10px;">导出</el-button>-->

            <!--            <el-button type="warning" style="margin-right: 10px;">房屋配置</el-button>-->

            <!--            <el-button type="danger">批量删除</el-button>-->

          </el-row>

        </div>

      </el-col>

    </el-row>

    <el-row :gutter="20">



      <el-col :span="24">

        <div class="grid-content bg-purple">
          <el-table v-loading="loading" :data="residentsqrList" @selection-change="handleSelectionChange">
            <el-table-column fixed prop="id" label="序号">
            </el-table-column>
            <el-table-column prop="name" label="姓名">
            </el-table-column>
            <el-table-column label="图片">
              <template slot-scope="scope">
                <image-preview :src="scope.row.photoInfo" :width="50" :height="50" />
              </template>

            </el-table-column>
            <el-table-column prop="typeName" label="住户类型">
            </el-table-column>
            <el-table-column prop="documentName" label="证件类型">
            </el-table-column>
            <el-table-column prop="documentNumber" label="证件号">
            </el-table-column>
            <el-table-column prop="phoneNumber" label="手机号">
            </el-table-column>
            <el-table-column prop="buildingName" label="楼栋">
            </el-table-column>
            <el-table-column prop="unitName" label="单元">
            </el-table-column>
            <el-table-column prop="floorName" label="楼层">
            </el-table-column>
            <el-table-column prop="roomsName" label="房间号">
            </el-table-column>
            <el-table-column prop="addTime" label="申请时间">
            </el-table-column>
            <el-table-column label="状态">

              <template slot-scope="scope">
                {{ scope.row.auditStatus === 2 ? "待审核" : scope.row.auditStatus === 1 ? "已通过" : "已拒绝" }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="handleUpdate(scope.row)">
                  {{ scope.row.auditStatus === 1 || scope.row.auditStatus === 3 ? '详情' : '审核' }}

                </el-button>

              </template>
            </el-table-column>
          </el-table>



        </div>

      </el-col>

    </el-row>




<!--    分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>


    <!-- 审核详情弹框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <span style="font-weight: bold; font-size: 16px">基本信息</span>
      <hr>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :spam="24">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name" placeholder="请输入姓名" :disabled="form.id !== null" style="width: 260px;" />
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-input v-model="form.gender==1?'男':'女'" placeholder="请输入性别" :disabled="form.id !== null" style="width: 260px;" />
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="房屋" prop="documentTypeId">
            <el-input v-model="form.roomsName" :disabled="form.id !== null" style="width: 260px;" />
          </el-form-item>
          <el-form-item label="手机号" prop="phoneNumber">
            <el-input v-model="form.phoneNumber" placeholder="请输入手机号" :disabled="form.id !== null"
              style="width: 260px;" />
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="证件类型" prop="documentTypeId">
            <el-input v-model="form.documentName" placeholder="请输入房号" :disabled="form.id !== null"
              style="width: 260px;" />
          </el-form-item>
          <el-form-item label="证件号" prop="documentNumber">
            <el-input v-model="form.documentNumber" placeholder="请输入证件号码" :disabled="form.id !== null"
              style="width: 260px;" />
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="住户类型" prop="residentTypeId">
            <el-input v-model="form.typeName" placeholder="请输入住户类型" :disabled="form.id !== null"
              style="width: 260px;" />
          </el-form-item>
          <el-form-item label="住户标签" prop="residentLabelId">
            <el-input v-model="form.labelName" placeholder="请输入住户标签" :disabled="form.id !== null"
              style="width: 260px;" />
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="照片信息" prop="photoInfo">
            <image-upload v-model="form.photoInfo" :disabled="form.id !== null" />
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="审核状态" prop="auditStatus" v-if="shzt">
            <el-radio v-model="form.auditStatus" label="1"
              @change="handleAccessControlStatusChange(form.auditStatus)">通过</el-radio>
            <el-radio v-model="form.auditStatus" label="3"
              @change="handleAccessControlStatusChange(form.auditStatus)">拒绝</el-radio>
          </el-form-item>

          <el-form-item label="审核状态"  v-if="form.auditStatus === 1 || form.auditStatus === 3">
            <span>{{ form.auditStatus === 1 ? '已通过' : '已拒绝' }}</span>
          </el-form-item>
        </el-row>
<!--        拒绝原因-->
        <el-row>
          <el-form-item label="拒绝原因" prop="rejectReason" v-if="jjyy">
              <el-input v-model="form.rejectReason"></el-input>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item label="门禁权限" prop="residentsAccessId" v-if="residentsAccesstf">
            <el-select v-model="form.residentsAccessId" placeholder="请选择">
              <el-option label="1个月" value="1"></el-option>
              <el-option label="3个月" value="2"></el-option>
              <el-option label="半年" value="3"></el-option>
              <el-option label="1年" value="4"></el-option>
              <el-option label="3年" value="5"></el-option>
              <el-option label="永久" value="6"></el-option>
              <el-option label="不激活" value="7"></el-option>
            </el-select>
          </el-form-item>
        </el-row>


        <el-row>
          <el-form-item label="激活时长" v-if="form.auditStatus===1">
            <span>{{ form.residentsAccessId == 1 ? '1个月' :form.residentsAccessId==2?'3个月':form.residentsAccessId==3?'半年':form.residentsAccessId==4?'1年':form.residentsAccessId==5?'3年':form.residentsAccessId==6?'永久':'不激活'}}</span>
          </el-form-item>
        </el-row>

<!--        拒绝原因的回显-->
        <el-row>
          <el-form-item label="拒绝原因" v-if="form.auditStatus===3">
            <span>{{form.rejectReason}}</span>
          </el-form-item>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listResidentsqr, getResidentsqr, delResidentsqr, addResidentsqr, updateResidentsqr, listResidentsTypes,listDocumentTypes } from "@/api/residentsqr/residentsqr/residentsqr";

export default {
  name: "Residentsqr",
  data() {
    return {
      //拒绝原因的显示或隐藏
      jjyy:false,
      //证件类型
      documentTypeList:[],
      //住户类型
      residentTypeList: [],
      shzt: false,
      residentsAccesstf: false,
      formvo: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 住户确定表格数据
      residentsqrList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        //姓名
        name: "",
        //住户状态
        documentTypeId: "",
        //证件号
        documentNumber: "",
        //证件类型
        residentTypeId: "",
        // 手机号
        phoneNumber: "",
        //状态
        auditStatus: ""
      },
      // 表单参数
      form: {
        auditStatus:null,
      },
      // 表单校验
      rules: {
        auditStatus: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
        residentsAccessId: [
          { required: true, message: "门禁权限不能为空", trigger: "change" }
        ],
        rejectReason: [
          { required: true, message: "拒绝原因不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
     this.getList();
     this.findList();

  },
  methods: {
    /**详情审核的确定按钮*/
    submitForm(){
        this.open=false;
        if(this.jjyy==false &&this.residentsAccesstf==false){
            if(this.form.auditStatus==2){
              this.open=true;
              this.$message.warning("请审核")
            }
        }else{
          if(this.form.auditStatus==1){
            if(this.form.residentsAccessId!=null){
              updateResidentsqr(this.form).then(res=>{
                this.getList();
              })
              this.$message("已通过")
            }else{
              this.$message.warning("请选择门禁权限");
              this.open=true;
            }
          }
          if(this.form.auditStatus==3){
            if(this.form.rejectReason!=null){
              updateResidentsqr(this.form).then(res=>{
                this.getList();
              })
              this.$message("已拒绝")
            }else{
              this.$message.warning("请选择拒绝原因");
              this.open=true;
            }
          }
        }



    },
    /** 详情和审核按钮*/
    handleAccessControlStatusChange(auditStatus){
      if(auditStatus==1){
        this.jjyy=false;
        this.residentsAccesstf=true;

      }else if (auditStatus==3){
        this.jjyy=true;
        this.residentsAccesstf=false;
        this.form.rejectReason=null;
      }
    },
    /**详情审核按钮*/
    handleUpdate(row) {

      getResidentsqr(row.id).then(res => {
        this.jjyy=false;
        this.shzt=false;
        if(row.auditStatus==2){
          this.shzt=true;
        }
        this.residentsAccesstf=false;
        this.open=true;
        this.form=res;
        this.from.roomsName=res.roomsName;
        this.from.documentName=res.documentName;
        this.from.typeName=res.typeName;
        this.form.labelName=res.labelName;

      });
    },
    /**重置按钮*/
    chongzhi(){
      this.queryParams={ pageNum: 1,
        pageSize: 10,
        //姓名
        name: "",
        //住户状态
        documentTypeId: "",
        //证件号
        documentNumber: "",
        //证件类型
        residentTypeId: "",
        // 手机号
        phoneNumber: "",
        //状态
        auditStatus: ""};
      // this.getList();
    },
    /** 条件查询住户类型表 */
    findList(){
      listResidentsTypes().then(res => {
        this.residentTypeList = res;
      });
      listDocumentTypes().then(res=>{
        this.documentTypeList=res;
      })

    },
    /** 分页*/
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryParams.pageSize=val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryParams.pageNum=val;
      this.getList();
    },
    /** 查询住户确定列表 */
    getList() {
      this.loading = true;
      listResidentsqr(this.queryParams).then(res => {
        this.residentsqrList = res.records;
        this.total = res.total;
        this.loading = false;
      });
    },





    // // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // // 表单重置
    // reset() {
    //   this.form = {
    //     id: null,
    //     name: null,
    //     gender: null,
    //     documentTypeId: null,
    //     documentNumber: null,
    //     residentTypeId: null,
    //     phoneNumber: null,
    //     residentLabelId: null,
    //     relationshipWithHead: null,
    //     photoInfo: null,
    //     accessControlStatus: null,
    //     residentsAccessid: null,
    //     licensePlate: null,
    //     partnerCompany: null,
    //     occupationCategory: null,
    //     employer: null,
    //     addTime: null,
    //     addedBy: null,
    //     appUsageStatus: null,
    //     auditStatus: null
    //   };
    //   this.resetForm("form");
    // },
    // /** 搜索按钮操作 */
    // handleQuery() {
    //   this.queryParams.pageNum = 1;
    //   this.getList();
    // },
    // /** 重置按钮操作 */
    // resetQuery() {
    //   this.resetForm("queryParams");
    //   this.handleQuery();
    // },
    // // // 多选框选中数据
    // // handleSelectionChange(selection) {
    // //   this.ids = selection.map(item => item.id)
    // //   this.single = selection.length!==1
    // //   this.multiple = !selection.length
    // // },
    // /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加住户确定";
    // },
    // /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getResidentsqr(id).then(response => {
    //     this.shzt = false;
    //     this.residentsAccesstf = false;
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "审核";
    //     if (this.form.auditStatus == 2) {
    //       this.shzt = true;
    //     }
    //   });
    // },
    // /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateResidentsqr(this.form).then(response => {
    //           // this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addResidentsqr(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    // // /** 删除按钮操作 */
    // // handleDelete(row) {
    // //   const ids = row.id || this.ids;
    // //   this.$modal.confirm('是否确认删除住户确定编号为"' + ids + '"的数据项？').then(function() {
    // //     return delResidentsqr(ids);
    // //   }).then(() => {
    // //     this.getList();
    // //     this.$modal.msgSuccess("删除成功");
    // //   }).catch(() => {});
    // // },
    // // /** 导出按钮操作 */
    // // handleExport() {
    // //   this.download('residentsqr/residentsqr/export', {
    // //     ...this.queryParams
    // //   }, `residentsqr_${new Date().getTime()}.xlsx`)
    // // }
  }
};
</script>
<style>
/* 半体 */



.grid-content {

  border-radius: 4px;

  min-height: 36px;

  box-shadow: 0 4px 6px rgba(61, 61, 61, 0.1), 0 -4px 6px rgba(61, 61, 61, 0.1),

    -4px 0 6px rgba(61, 61, 61, 0.1), 2px 0 6px rgba(61, 61, 61, 0.1);

}

.bg-purple {

  background: #ffffff;

  height: 600px;

  margin-top: 15px;

}

/* 按钮 */

.el-row {

  display: flex;

  justify-content: flex-start;

}



.el-button+.el-button,

.el-upload+.el-button {

  margin-left: 10px;

}



/* form表单 */

.custom-select .el-input__inner {

  width: 140px;

}



/* header */

.bg-purple-dark {

  background: #ffffff;

  margin-top: 25px;

  /* height: 100px; */

  padding-top: 10px;

  padding-bottom: 10px;

  padding-left: 20px;

  box-shadow: 0 4px 6px rgba(61, 61, 61, 0.1), 0 -4px 6px rgba(61, 61, 61, 0.1),

    -4px 0 6px rgba(61, 61, 61, 0.1), 2px 0 6px rgba(61, 61, 61, 0.1);

}



.grid-content {

  border-radius: 4px;

  min-height: 36px;

}
</style>
